package com.jiezheng.huinong.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jiezheng.huinong.dao.ApplyManageDAOImpl;
import com.jiezheng.huinong.model.ApplyIndex;
import com.jiezheng.huinong.model.IndexInfo;
import com.jiezheng.huinong.model.OperationStatus;
import com.jiezheng.huinong.util.JqPage;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ApplyIndexAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -5369775930240362713L;
	private JqPage pageInfo = new JqPage();
	private List<ApplyIndex> applyIndexes = new ArrayList<ApplyIndex>();
	private String sord = "";
	private String sidx = "";

	// 查询条件
	private IndexInfo cond = new IndexInfo();

	public String toAuditApplyInfo() {

		//
		// !!!特此说明，此处的deptNo是指申请的DeptNo,此处使用，仅为搜索条件方便，下同
		//
		cond.setDeptNo(deptNoCondition());

		Integer total = (new ApplyManageDAOImpl())
				.getTotalToAuditApplyNum(cond);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		applyIndexes = (new ApplyManageDAOImpl()).getToAuditApplyInfo(cond,
				startRow, pageInfo.getRows());
		return SUCCESS;
	}

	public String noAuditApplyInfo() {
		//
		// !!!特此说明，此处的deptNo是指申请的DeptNo,此处使用，仅为搜索条件方便，下同
		//
		cond.setDeptNo(deptNoCondition());
		
		int status = OperationStatus.NO_AUDIT;
		Integer total = (new ApplyManageDAOImpl()).getTotalApplyIndexNum(cond,
				status);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		applyIndexes = (new ApplyManageDAOImpl()).getApplyIndexInfo(cond,
				status, startRow, pageInfo.getRows());
		return SUCCESS;
	}

	public String auditApplyInfo() {
		//
		// !!!特此说明，此处的deptNo是指申请的DeptNo,此处使用，仅为搜索条件方便，下同
		//
		cond.setDeptNo(deptNoCondition());
		
		int status = OperationStatus.FINAL_AUDITED;
		Integer total = (new ApplyManageDAOImpl()).getTotalApplyIndexNum(cond,
				status);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		applyIndexes = (new ApplyManageDAOImpl()).getApplyIndexInfo(cond,
				status, startRow, pageInfo.getRows());
		return SUCCESS;
	}

//	public String noFinalAuditApplyInfo() {
//		//
//		// !!!特此说明，此处的deptNo是指申请的DeptNo,此处使用，仅为搜索条件方便，下同
//		//
//		cond.setDeptNo(deptNoCondition());
//		
//		int status = OperationStatus.AUDITED;
//		Integer total = (new ApplyManageDAOImpl()).getTotalApplyIndexNum(cond,
//				status);
//		Integer totalPage = (Integer) (total / pageInfo.getRows())
//				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
//		pageInfo.setTotalPage(totalPage);
//		pageInfo.setRecord(total);
//		// 取得当前页
//		Integer curPage = pageInfo.getPage();
//		Integer startRow = (curPage - 1) * pageInfo.getRows();
//
//		applyIndexes = (new ApplyManageDAOImpl()).getApplyIndexInfo(cond,
//				status, startRow, pageInfo.getRows());
//		return SUCCESS;
//	}
//
//	public String finalAuditApplyInfo() {
//		//
//		// !!!特此说明，此处的deptNo是指申请的DeptNo,此处使用，仅为搜索条件方便，下同
//		//
//		cond.setDeptNo(deptNoCondition());
//		
//		int status = OperationStatus.FINAL_AUDITED;
//		Integer total = (new ApplyManageDAOImpl()).getTotalApplyIndexNum(cond,
//				status);
//		Integer totalPage = (Integer) (total / pageInfo.getRows())
//				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
//		pageInfo.setTotalPage(totalPage);
//		pageInfo.setRecord(total);
//		// 取得当前页
//		Integer curPage = pageInfo.getPage();
//		Integer startRow = (curPage - 1) * pageInfo.getRows();
//
//		applyIndexes = (new ApplyManageDAOImpl()).getApplyIndexInfo(cond,
//				status, startRow, pageInfo.getRows());
//		return SUCCESS;
//	}

	public String noPayApplyInfo() {
		//
		// !!!特此说明，此处的deptNo是指申请的DeptNo,此处使用，仅为搜索条件方便，下同
		//
		cond.setDeptNo(deptNoCondition());
		
		int status = OperationStatus.SUBMITED;
		Integer total = (new ApplyManageDAOImpl()).getTotalApplyIndexNum(cond,
				status);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		applyIndexes = (new ApplyManageDAOImpl()).getApplyIndexInfo(cond,
				status, startRow, pageInfo.getRows());
		return SUCCESS;
	}

	public String payApplyInfo() {
		//
		// !!!特此说明，此处的deptNo是指申请的DeptNo,此处使用，仅为搜索条件方便，下同
		//
		cond.setDeptNo(deptNoCondition());
		
		int status = OperationStatus.EXECUTED;
		Integer total = (new ApplyManageDAOImpl()).getTotalApplyIndexNum(cond,
				status);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		applyIndexes = (new ApplyManageDAOImpl()).getApplyIndexInfo(cond,
				status, startRow, pageInfo.getRows());
		return SUCCESS;
	}

	private String deptNoCondition() {
		// 检验当前用户的部门
		Map<String, Object> session = ActionContext.getContext().getSession();
		String deptNo = (String) session.get("deptNo");

		// 财政所用户处理
		if (deptNo.startsWith("2")) {
			return deptNo;
		}

		// 国库股/中国人民银行应可以看到所有申请
		else if (deptNo.equals("003") || deptNo.equals("800")) {
			deptNo = "";
		}
		return deptNo;
	}

	public JqPage getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(JqPage pageInfo) {
		this.pageInfo = pageInfo;
	}

	public List<ApplyIndex> getApplyIndexes() {
		return applyIndexes;
	}

	public void setApplyIndexes(List<ApplyIndex> applyIndexes) {
		this.applyIndexes = applyIndexes;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public IndexInfo getCond() {
		return cond;
	}

	public void setCond(IndexInfo cond) {
		this.cond = cond;
	}
}
